package au.edu.unsw.editingtool.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import au.edu.unsw.editingtool.db.DBManager;

/**
 * Servlet implementation class TestServelt
 */
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public TestServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//doRequest(request, response);
		doSessionTest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//doRequest(request, response);
		doSessionTest(request, response);
	}

	//Test Session Listener with interval, and timeout.
	protected void doSessionTest(HttpServletRequest req,
			HttpServletResponse res) throws ServletException, IOException {
	    res.setContentType("text/plain");
	    PrintWriter out = res.getWriter();

	    // Get the current session object, create one if necessary
	    HttpSession session = req.getSession();
	    if (session.isNew())
	    	out.println("new session");
	    else
	    	out.println("old session");
	    	    
	    // test about httpsessionbindinglistener
	    if (session.isNew())
	    	session.setAttribute("sessionbinding", new SessionBindingListener("session listener"));
	    
	    
	    out.println("This page intentionally left blank");
	    out.println(this.getServletContext().getRealPath(""));
	    out.println("in testservlet: session id is " + session.getId());
	    session.setMaxInactiveInterval(20);
	    
	    Catch catcher = new Catch();
	    catcher.service(req, res);
	}
	
	protected void doRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String element = "";
		PrintWriter pw = response.getWriter();
		for (Enumeration<String> e = request.getParameterNames(); e
				.hasMoreElements();) {
			element = (String) e.nextElement();
			pw.println(element + "\t\t" + request.getParameter(element));
		}
		pw.flush();
		// response.setStatus(HttpServletResponse.SC_NO_CONTENT);
	}
	
	//run when user session finished.
	public void destroy() {
		this.getServletContext().log("user session completed");
	}
}
